﻿using System;
using Prometheus.UIUtility;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
using System.Windows;
#endif


namespace Prometheus.Binding.Validation.Controls
{
    /// <summary>
    /// Show first error from prometheus binding system
    /// By default tries to search proForm at visual tree
    /// </summary>
    public partial class proErrorBlock
    {
        private proValidationForm _baseControl;
        private string _baseControlName;
        private bool _isLoaded;

        public proErrorBlock()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        /// <summary>
        /// null - binding to all groups
        /// </summary>
        public string BindingGroupName { get; set; }

        public string BaseControlName
        {
            get { return _baseControlName; }
            set
            {
                if (_isLoaded)
                    UnbindToBaseControl(FindName(_baseControlName) as proValidationForm);
                _baseControlName = value;
                if (_isLoaded)
                    BindToBaseControl(FindName(_baseControlName) as proValidationForm);
            }
        }

        public proForm BaseControl
        {
            get { return (proForm) _baseControl; }
            set
            {
                if (_isLoaded)
                    UnbindToBaseControl(_baseControl);
                _baseControl = value;
                if (_isLoaded)
                    BindToBaseControl(_baseControl);
            }
        }


        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            _isLoaded = true;


            proValidationForm baseControl = null;

            if (!string.IsNullOrEmpty(BaseControlName))
                baseControl = (proValidationForm) FindName(BaseControlName);
            if (BaseControl != null)
                baseControl = BaseControl;
            if (baseControl == null)
                baseControl = this.FindAncestor<proValidationForm>();

            BindToBaseControl(baseControl);
        }

        private void BindToBaseControl(proValidationForm baseControl)
        {
            if (baseControl != null)
            {
                baseControl.FirstErrorChanged += FirstErrorChanged;
                UpdateError(baseControl);
            }
        }


        private void UnbindToBaseControl(proValidationForm baseControl)
        {
            if (baseControl != null)
            {
                baseControl.FirstErrorChanged -= FirstErrorChanged;
                UpdateError(baseControl);
            }
        }

        private void FirstErrorChanged(object sender, EventArgs e)
        {
            UpdateError((proValidationForm) sender);
        }

        private void UpdateError(proValidationForm sender)
        {
            txt.Text = sender.GetFirstError(BindingGroupName);
        }
    }
}